# Author: jumpingzp
# Email: jumpingzp@gmail.com

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from meet001.mailtest.forms import SendMailForm

def send_mail(request):
  if request.method == "POST":
    return _send_post(request)
  else:
    return _send_get(request)
    
def _send_post(request):
  errors=[]
  form = SendMailForm(request.POST)
  if not form.is_valid():
    for key in form.errors.keys():
      errmsg = key + ': '
      errors.append(errmsg)
      for value in form.errors[key]:
        errmsg = value
        errors.append(errmsg)
    return render_to_response('sendmail.html', {'errors':errors})
  form.send()
  return render_to_response('sendmail.html', {'errors':['Your mail has been sent successfully!']})

def _send_get(request):
  return render_to_response('sendmail.html', {})
    
